package base

import "sort"

/*
* @author: hjz
* @time: 2025/4/26 18:00
* @  每次先对字符串排序,再去插入到对应的数组里
 */

func groupAnagrams(strs []string) [][]string {
	m := make(map[string]int, len(strs))
	res := make([][]string, 0, len(strs)>>1)
	for _, str := range strs {
		// 排序
		tmp := []rune(str)
		sort.Slice(tmp, func(i, j int) bool {
			return tmp[i] < tmp[j]
		})
		if i, ok := m[string(tmp)]; ok {
			res[i] = append(res[i], str)
		} else {
			m[string(tmp)] = len(res)
			res = append(res, []string{str})
		}
	}
	return res
}
